//
//  RegisterViewController.m
//  JinMaJiaDemo
//
//  Created by GripMeMory on 16/1/12.
//  Copyright © 2016年 GripMeMory. All rights reserved.
//

#import "RegisterViewController.h"
#import "JMJLoginTextField.h"
#import "VerifyViewController.h"

#define kSegmentedControlHeight 90 / 2
#define kSegmentedIndicatorHeight 3
#define kSegmentedIndicatorLeftMargin 10
#define kTextFieldTopMargin 50 / 2
#define kTextFieldLeftMargin 50 / 2
#define kTextFieldRightMargin 65 / 2
#define kTextFieldSeparateMargin 20 / 2
#define kLabelWidth 165 / 2
#define kLabelHeight 60 / 2

@interface RegisterViewController () <UITextFieldDelegate>

{
    UISegmentedControl *_sc;
    UIScrollView *_scrollView;
    UIView *_indicator;
    
    UILabel *_entityLabel;
    JMJNormalTextField *_entityTextField;
    UIView *_entitySeparateView;
    JMJNormalTextField *_userNameTextField;
    JMJNormalTextField *_userPhoneNumberTextField;
    JMJNormalTextField *_userEmailTextField;
    
    NSArray *_viewArray;
}

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self customNavBar];
    [self customSegmentControl];
    [self customMainView];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.tabBarController.tabBar.hidden = NO;
    if (_sc.selectedSegmentIndex == 1) {
        _sc.selectedSegmentIndex = 0;
        for (UIView *view in _viewArray) {
            CGRect rect = view.frame;
            view.frame = CGRectMake(rect.origin.x, rect.origin.y - kTextFieldSeparateMargin * 2 - kLabelHeight, rect.size.width, rect.size.height);
            _entityLabel.hidden = YES;
            _entityTextField.hidden = YES;
            _entitySeparateView.hidden = YES;
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)customNavBar{
    self.title = @"注册1/3";
    UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"返回"] style:UIBarButtonItemStylePlain target:self action:@selector(bbiClick:)];
    self.navigationItem.leftBarButtonItem = bbi;
}

- (void)customSegmentControl{
    _sc = [[UISegmentedControl alloc] initWithItems:@[@"个人注册", @"企业注册"]];
    _sc.frame = CGRectMake(0, kNavigationBarHeight, UISCREENWIDTH, kSegmentedControlHeight);
    _sc.tintColor = [UIColor clearColor];
    _sc.selectedSegmentIndex = 0;
    [_sc setTitleTextAttributes:@{NSForegroundColorAttributeName:RGB_COLOR(@"#d91f00")} forState:UIControlStateSelected];
    [_sc setTitleTextAttributes:@{NSForegroundColorAttributeName:RGB_COLOR(@"#c9c9c9")} forState:UIControlStateNormal];
    [_sc addTarget:self action:@selector(segmentedControlSelected) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_sc];
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(_sc.left + kSegmentedIndicatorLeftMargin, _sc.bottom, _sc.width - 2 * kSegmentedIndicatorLeftMargin, kSegmentedIndicatorHeight)];
    _scrollView.backgroundColor = RGB_COLOR(@"#c9c9c9");
    _scrollView.scrollEnabled = YES;
    
    _indicator = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _sc.width / 2 - kSegmentedIndicatorLeftMargin, kSegmentedIndicatorHeight)];
    _indicator.backgroundColor = RGB_COLOR(@"#d91f00");
    [_scrollView addSubview:_indicator];
    
    [self.view addSubview:_scrollView];
}

- (void)customMainView{
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(_scrollView.left, _scrollView.bottom + kTextFieldTopMargin, kLabelWidth, kLabelHeight)];
    label.text = @"联系人姓名";
    label.adjustsFontSizeToFitWidth = YES;
    label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:label];
    
    _userNameTextField = [[JMJNormalTextField alloc] initWithFrame:CGRectMake(label.right + kTextFieldLeftMargin, _scrollView.bottom + kTextFieldLeftMargin, _scrollView.right - label.right - kTextFieldLeftMargin, kLabelHeight)];
    _userNameTextField.layer.cornerRadius = 3;
    _userNameTextField.layer.borderWidth = 2;
    _userNameTextField.layer.borderColor = [RGB_COLOR(@"#c9c9c9") CGColor];
    _userNameTextField.delegate = self;
    [self.view addSubview:_userNameTextField];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(_scrollView.left, _userNameTextField.bottom + kTextFieldSeparateMargin, _scrollView.width, 1)];
    view.backgroundColor = RGB_COLOR(@"#c9c9c9");
    [self.view addSubview:view];
    
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(label.left, view.bottom + kTextFieldSeparateMargin, kLabelWidth, kLabelHeight)];
    label2.text = @"手机号码";
    label2.lineBreakMode = NSLineBreakByClipping;
    label2.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
    label2.textAlignment = NSTextAlignmentJustified;
    label2.numberOfLines = 1;
    [self.view addSubview:label2];
    
    _userPhoneNumberTextField = [[JMJNormalTextField alloc] initWithFrame:CGRectMake(_userNameTextField.left, label2.top, _userNameTextField.width, _userNameTextField.height)];
    _userPhoneNumberTextField.layer.cornerRadius = 3;
    _userPhoneNumberTextField.layer.borderWidth = 2;
    _userPhoneNumberTextField.layer.borderColor = [RGB_COLOR(@"#c9c9c9") CGColor];
    _userPhoneNumberTextField.delegate = self;
    [self.view addSubview:_userPhoneNumberTextField];
    
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(_scrollView.left, _userPhoneNumberTextField.bottom + kTextFieldSeparateMargin, _scrollView.width, 1)];
    view2.backgroundColor = RGB_COLOR(@"#c9c9c9");
    [self.view addSubview:view2];
    
    UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(label.left, view2.bottom + kTextFieldSeparateMargin, kLabelWidth, kLabelHeight)];
    NSString *string = @"邮箱(选填)";
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:string];
    [attributeString setAttributes:@{NSForegroundColorAttributeName:RGB_COLOR(@"#d91f00")} range:NSMakeRange(2, string.length - 2)];
    label3.attributedText = attributeString;
    label3.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:label3];
    
    _userEmailTextField = [[JMJNormalTextField alloc] initWithFrame:CGRectMake(_userNameTextField.left, label3.top, _userNameTextField.width, _userNameTextField.height)];
    _userEmailTextField.layer.cornerRadius = 3;
    _userEmailTextField.layer.borderWidth = 2;
    _userEmailTextField.layer.borderColor = [RGB_COLOR(@"#c9c9c9") CGColor];
    _userEmailTextField.delegate = self;
    [self.view addSubview:_userEmailTextField];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(_scrollView.left, _userEmailTextField.bottom + kTextFieldTopMargin, _scrollView.width, kLabelHeight + 2 * kTextFieldSeparateMargin);
    [button setTitle:@"下一步,验证手机号" forState:UIControlStateNormal];
    button.backgroundColor = RGB_COLOR(@"#d91f00");
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.layer.cornerRadius = 5;
    [button addTarget:self action:@selector(registerButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    _entityLabel = [[UILabel alloc] initWithFrame:CGRectMake(_scrollView.left, _scrollView.bottom + kTextFieldTopMargin, kLabelWidth, kLabelHeight)];
    _entityLabel.text = @"企业机构名称";
    _entityLabel.adjustsFontSizeToFitWidth = YES;
    _entityLabel.textAlignment = NSTextAlignmentCenter;
    _entityLabel.hidden = YES;
    [self.view addSubview:_entityLabel];
    
    _entityTextField = [[JMJNormalTextField alloc] initWithFrame:CGRectMake(_entityLabel.right + kTextFieldLeftMargin, _scrollView.bottom + kTextFieldLeftMargin, _scrollView.right - _entityLabel.right - kTextFieldLeftMargin, kLabelHeight)];
    _entityTextField.textAlignment = NSTextAlignmentCenter;
    _entityTextField.layer.cornerRadius = 3;
    _entityTextField.layer.borderWidth = 2;
    _entityTextField.layer.borderColor = [RGB_COLOR(@"#c9c9c9") CGColor];
    _entityTextField.hidden = YES;
    [self.view addSubview:_entityTextField];
    
    _entitySeparateView = [[UIView alloc] initWithFrame:CGRectMake(_scrollView.left, _userNameTextField.bottom + kTextFieldSeparateMargin, _scrollView.width, 1)];
    _entitySeparateView.backgroundColor = RGB_COLOR(@"#c9c9c9");
    [self.view addSubview:_entitySeparateView];
    
    _viewArray = @[view, view2, label, label2, label3, _userNameTextField, _userPhoneNumberTextField, _userEmailTextField, button];
}

#pragma mark - Actions

- (void)segmentedControlSelected{
    _scrollView.contentOffset = CGPointMake((_sc.width / 2  - kSegmentedIndicatorLeftMargin) * -(_sc.selectedSegmentIndex % 2), 0);
    
    if (_sc.selectedSegmentIndex == 1) {
        for (UIView *view in _viewArray) {
            CGRect rect = view.frame;
            view.frame = CGRectMake(rect.origin.x, rect.origin.y + kTextFieldSeparateMargin * 2 + kLabelHeight, rect.size.width, rect.size.height);
            _entityTextField.hidden = NO;
            _entityLabel.hidden = NO;
            _entitySeparateView.hidden = NO;
        }
    }else{
        for (UIView *view in _viewArray) {
            CGRect rect = view.frame;
            view.frame = CGRectMake(rect.origin.x, rect.origin.y - kTextFieldSeparateMargin * 2 - kLabelHeight, rect.size.width, rect.size.height);
            _entityLabel.hidden = YES;
            _entityTextField.hidden = YES;
            _entitySeparateView.hidden = YES;
        }
    }
}

- (void)bbiClick:(UIBarButtonItem *)bbi{
    [self.navigationController popViewControllerAnimated:NO];
}

- (void)registerButtonClick{
    VerifyViewController *vvc = [[VerifyViewController alloc] init];
    [self.navigationController pushViewController:vvc animated:NO];
}

@end
